<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="/css/font.css">
    <link rel="stylesheet" href="/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script src="/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/js/xadmin.js"></script>
</head>
<body>
<div class="x-nav">
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
</div>
<xblock>
    <form action="/user/excel">
    <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
    <button class="layui-btn" onclick="x_admin_show('添加用户','/user/add')"><i class="layui-icon"></i>添加</button>
    <button class="layui-btn"  type="submit"><i class="layui-icon"></i>excel导出</button>
    </form>
</xblock>
<table class="layui-table">
    <thead>
    <tr>
        <th>
            <div class="layui-unselect header layui-form-checkbox" lay-skin="primary" id="select"><i class="layui-icon">&#xe605;</i></div>
        </th>
        <th hidden="hidden">用户id</th>
        <th >姓名</th>
        <th>用户名</th>
        <th>身份证号码</th>
        <th>生日</th>
        <th>地址</th>
        <th>电话</th>
        <th>状态</th>
        <th>操作</th>
    </thead>
    <tbody>
    <tr th:each="x:${list}">
        <td>
            <input type="checkbox" name="selectAll" lay-filter="all" th:value="${x.userId}">
            <!--<div class="layui-unselect layui-form-checkbox" lay-skin="primary"  ><i class="layui-icon">&#xe605;</i></div>-->
        </td>
        <td hidden="hidden" th:text="${x.userId}"></td>
        <td th:text="${x.userLocalName}"></td>
        <td th:text="${x.userName}"></td>
        <td th:text="${x.userIdenty}"></td>
        <td th:text="${x.userBirth}"></td>
        <td th:text="${x.userAddr}"></td>
        <td th:text="${x.userTel}"></td>
        <td class="td-status" th:if="${x.userStatus==1}">
            <span class="layui-btn layui-btn-normal layui-btn-mini">启用</span>
        </td>
        <td class="td-status" th:if="${x.userStatus==0}">
            <span class="layui-btn layui-btn-normal layui-btn-mini layui-btn-disabled">停用</span>
        </td>

        <td class="td-manage" th:if="${x.userStatus==1}">
            <a onclick="member_stop(this,'10001')" href="javascript:;" title="启用">
                <i class="layui-icon">&#xe601;</i>
            </a>
            <a title="编辑"  onclick="update(this)" href="javascript:;">
                <i class="layui-icon">&#xe642;</i>
            </a>
            <a title="删除" onclick="member_del(this)" href="javascript:;">
                <i class="layui-icon">&#xe640;</i>
            </a>
        </td>
        <td class="td-manage" th:if="${x.userStatus==0}">
            <a onclick="member_stop(this,'10001')" href="javascript:;"  title="停用">
                <i class="layui-icon">&#xe601;</i>
            </a>
            <a title="编辑"  onclick="update(this)" href="javascript:;">
                <i class="layui-icon">&#xe642;</i>
            </a>
            <a title="删除" onclick="member_del(this)" href="javascript:;">
                <i class="layui-icon">&#xe640;</i>
            </a>
        </td>
    </tr>
    </tbody>
</table>
<div class="page">
<div>
        <a class="prev" th:href="@{/user/allPageList(page=${prePage})}">&lt;&lt;</a>
        <a class="num" href="/user/allPageList">首页</a>
        <span class="current" th:text="${curPage}"></span>
        <a class="num" th:href="@{/user/allPageList(page=${totalPages})}">尾页</a>
        <a class="next" th:href="@{/user/allPageList(page=${nextPage})}">&gt;&gt;</a>
</div>
</div>
</div>
</body>
</html>
<script>
    //打开修改页面
    /*用户-编辑*/
    function update(obj) {
        var userId=$(obj).parents("tr").find("td").eq(1).text();
        var url ='/user/edit?userId='+userId
        x_admin_show('用户修改',url)
    }

// ------------------------------------------------------------------------------------------------

    /*用户-删除*/
    function member_del(obj){
        //获取要删除用户的编号userId
        var userId=$(obj).parents("tr").find("td").eq(1).text();
        layer.confirm('确认要删除吗？',function(index){
            $.ajax({
                url:"/user/del",
                async: false,
                type:"POST",
                dataType: "json",
                data:{"userId":userId},
                success: function(data){
                    if(data==true){
                        $(obj).parents("tr").remove();
                        layer.msg('已删除!',{icon:1,time:1000});
                    }else{
                        layer.msg('删除失败!',{icon: 1,time:1000});
                    }

                }
            })

        });
    }
</script>
<script>
    //全选
    $('input:checkbox[id="select"]').click(function(){
        if($(this).is(':checked')){
            $('input:checkbox').each(function(){
                $(this).prop("checked",true);
            });
        }else{
            $('input:checkbox').each(function(){
                $(this).prop("checked",false);
            });
        }
    });


    function member_stop(obj,id){
        //用户停用启用
        //更改按钮样式

        layer.confirm('确认要更改状态吗？',function(index){
            var userId =$(obj).parents("tr").find("td").eq(1).text();
            if($(obj).attr('title')=='启用'){
                //发异步把用户状态进行更改
                $.ajax({
                    url: "/user/show",
                    async: false,
                    type: "POST",
                    dataType: "json",
                    data: {
                        "userId":userId,
                        "userStatus":0
                    },
                    success: function (data) {
                        //放入了map中，调用info
                        if (data==true) {
                            $(obj).attr('title','停用')
                            $(obj).find('i').html('&#xe62f;');
                            $(obj).parents("tr").find(".td-status").find('span').addClass('layui-btn-disabled').html('停用');
                            layer.msg('已停用!',{icon: 5,time:1000});
                        }else {
                            layer.msg('停用失败!',{icon: 1,time:1000});
                        }
                    }
                })

            }else {
                $.ajax({
                    url: "/user/show",
                    async: false,
                    type: "POST",
                    dataType: "json",
                    data: {
                        "userId": userId,
                        "userStatus": 1
                    },
                    success: function (data) {
                        //放入了map中，调用info
                        if (data==true) {
                            $(obj).attr('title', '启用')
                            $(obj).find('i').html('&#xe601;');
                            $(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-disabled').html('启用');
                            layer.msg('已启用!', {icon: 6, time: 1000});
                        } else {
                            layer.msg('启用失败!', {icon: 1, time: 1000});
                        }
                    }
                });
            }
        })
    }
</script>
<script>
    //批量删除
    function delAll (obj) {
        //获取复选框选中的用户的uId
        // var data = tableCheck.getData().toString();
        // //选中的个数
        // var long = data.length;
        // var arr = [];

        var arr_box = [];
        $('input[name=selectAll]:checked').each(function() {
            arr_box.push($(this).val());
        });
        var s = arr_box.toString()
        console.log(arr_box)
        // //给数组赋值选中的id
        // for(var i=0;i<long;i++){
        //     //children('.td)为选中class为td的值，这个值为我们要选中的id.
        //     arr[i] = $(".layui-form-checked").not('.header').parents('tr').children('.td').eq(i).html();
        // }


        layer.confirm('确认要删除吗？'+arr_box,function(index){
            $.ajax({
                url:"/user/delAll",
                async: false,
                type:"POST",
                dataType: "json",
                data:{"userIds":s},
                success: function(data){
                    if(data==true){
                        layer.msg('删除成功', {icon: 1,time:1000});
                        $(".layui-form-checked").not('.header').parents('tr').remove();

                    }else{
                        layer.msg('删除失败!',{icon: 1,time:1000});
                    }

                }
            })

        });
    }
</script>
